package game;

import java.util.Date;

/**
 * A class representing the collision between two space objects. 
 * @author JeroenGillis
 *
 */
public class FutureCollision {

	private final SpaceObject object1;
	private final SpaceObject object2;
	private Date collisionMoment;
	//Garbagecolletor : futurecollision wijst naar spaceObjects die getermineerd kunnen zijn.

	public FutureCollision(SpaceObject object1, SpaceObject object2) throws IllegalStateException {
		this.object1 = object1;
		this.object2 = object2;
		if(isTerminated()) throw new IllegalStateException();
		
		update();
	}
	
	public void update() throws IllegalStateException {
		Date newDate = new Date();
		double timeToCollision = getObject1().getTimeToCollision(getObject2());
		if(timeToCollision == Double.POSITIVE_INFINITY)
			setCollisionMoment(null);
		else {
		newDate.setTime(newDate.getTime() + (long) (timeToCollision*1000) );
		setCollisionMoment(newDate);
		}
	}
	
	public Date getCollisionMoment() {
		return this.collisionMoment;
	}
	
	public double getTimeToCollision() {
		return ((double) (getCollisionMoment().getTime() - (new Date()).getTime()))/1000;
	}

	private void setCollisionMoment(Date collisionMoment) {
		this.collisionMoment = collisionMoment;
	}

	public SpaceObject getObject1() {
		return object1;
	}

	public SpaceObject getObject2() {
		return object2;
	}
	
	public boolean isTerminated() {
		return getObject1().isTerminated() ||  getObject2().isTerminated();
	}
	
	
}
